package com.lightsh.things;

import org.jbox2d.common.Vec2;
import com.lightsh.body.PolygonImpl;
import com.lightsh.gui.PhysicsScreen;
import com.lightsh.utility.GeometryUtility;

/**
 * 飞机的碎片物体，该物体不会与任何东西发生碰撞，并且会在出现一段时间后消失
 * @author Light
 *
 */
public class PlaneChip extends PolygonImpl{

	private final long mLastSecond;
	
	private long mPastUpdates;
	private float mVelocityAngle = 0;
	private float mVelocity = 0;
	
	public PlaneChip(PhysicsScreen screen, float x, float y, float density,
			float friction, float restitution, float rectangleSize, long lastSecond) {
		super(screen, x, y, density, friction, restitution);
		
		setDrawLevel(1);
		
		mLastSecond = lastSecond;
		mPastUpdates = 0;
		
		// 与任何物体都不会发生碰撞
		mPolygonDef.filter.groupIndex = 0;
		mPolygonDef.filter.categoryBits = 0;
		
		mBodyDef.linearDamping = 0.5f;		
		float width = rectangleSize;
		float height = rectangleSize;
		
		add(new Vec2(0, 0), false);
		add(new Vec2(width, 0), false);
		add(new Vec2(width, height), false);
		add(new Vec2(0, height), true);
	}
	
	/**
	 * 使用该方法来设置物体的初始速度信息,注意该方法必须在物体的{@link #create()}方法调用前调用才能生效
		 * @param velocity			初始的速度值
		 * @param angle				速度的角度，弧度做单位
	 */
	public void setVelocityInfo(float velocity, float angle){
		mVelocity = velocity;
		mVelocityAngle = angle;
	}

	@Override
	public void update() {
		super.update();
		mPastUpdates++;
		if(mPastUpdates * mParentScreen.getTimeStep() > mLastSecond){
			// FIXME 结束, 只需要destroy物体？
			mWorld.destroyBody(mBody);
		}
		
	}

	@Override
	public void create() {
		super.create();
		
		// 设置初始速度
		if(mVelocity != 0){
			mBody.setLinearVelocity(GeometryUtility.getTargetRadianPoint(0, 0, mVelocityAngle, mVelocity));
		}
		
	}

	
}
